<?php


if (isset($_GET['action']) && $_GET['action'] == "edit" && isset($_GET['attribute_id'])) {

	$attribute_id = $_GET['attribute_id'];

	$attribute_item = $attribute->get($attribute_id);

	$attribute_item = $attribute_item[0];

	$smarty->assign('attribute_item', $attribute_item);

	$smarty->assign('fileaction', "edit");

	$smarty->display('attribute_edit.tpl', $_SESSION['uid']);

}elseif (isset($_GET['action']) && $_GET['action'] == "new") {

	$smarty->assign('fileaction', "new");

	$smarty->display('attribute_edit.tpl', $_SESSION['uid']);

}elseif (isset($_GET['action']) && $_GET['action'] == "remove" && is_allowed(3))  {

	$attribute_id = $_GET['attribute_id'];

	$attribute->remove($attribute_id);

	$attribute = $attribute->get();

	$smarty->assign('attribute', $attribute);

	$smarty->assign('message', "The attribute has been removed!");

	$smarty->display('attribute_list.tpl', $_SESSION['uid']);

}elseif (isset($_POST['action']) && $_POST['action'] == "save") {


	$attribute_id 	= $_POST['attribute_id'];
	$name 			= $_POST['name'];
	$short_name		= $_POST['short_name'];
	$description 	= $_POST['description'];
	$type       	= $_POST['type'];


	if($_POST['fileaction'] == "edit") {
		if($_FILES["picture"]['error'] == 4) {

			$attribute_item = $attribute->get($attribute_id);

			$attribute_item = $attribute_item[0];

			$picture_link = $attribute_item['picture_link'];

		}
	} else {
		$picture_link = "";
	}



	if(!empty($_FILES["picture"])) {

		$downloaddir = $_SERVER['DOCUMENT_ROOT'] . "/images/attributes/";

		if(move_uploaded_file($_FILES["picture"]["tmp_name"], $downloaddir . $_FILES["picture"]["name"])) {

			$picture_link 		= $_FILES["picture"]["name"];

		} else {

		}
	}

	$attribute->save($attribute_id, $name, $short_name, $description, $type, $picture_link);

	$smarty->assign('message', "The attribute has been changed/added!");

	$smarty->display('attribute_edit.tpl', $_SESSION['uid']);

} else {

	$attribute = $attribute->get();

	$smarty->assign('attribute', $attribute);

	$smarty->display('attribute_list.tpl', $_SESSION['uid']);
}





?>
